package com.imay.live.activity.suggest;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.activity.MainActivity2;
import com.imay.live.activity.ProfileActivity;
import com.imay.live.activity.discovery.FollowControl;
import com.imay.live.adapter.SuggestAdapter;
import com.imay.live.helper.H5Helper;
import com.imay.live.model.UserModel;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.Result;
import com.imay.live.proto.ResultCode;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.R;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

/**
 * Created by chan on 2017/5/3.
 */

public class SuggestActivity extends BaseRequestActivity implements View.OnClickListener {
    private String loadRecommendUserCommand;

    private RecyclerView mRecyclerView;
    private SuggestAdapter mAdapter;

    private Context mContext;

    FollowControl followControl;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, SuggestActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dynamics_suggest;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mContext = this;

        getTitleBar().setTitle(R.string.str_dynamics_suggest);
        getTitleBar().getLeftView().removeAllViews();

        followControl = new FollowControl();


        initDatas();
        initWidget();
        initListener();
    }

    private void initListener() {
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                UserModel item = mAdapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.iv_item_suggest_follow:
                        if(!item.isMyFollow()){
                            followControl.followChanged(view.getContext(), item, false);
                            view.setSelected(true);
                            view.setClickable(false);
                            //mAdapter.notifyItemChanged(position);
                        }
                        break;
                    case R.id.civ_item_suggest_img:
                        ProfileActivity.start(view.getContext(), item.getUid());
                        break;
                }
            }
        });

        $(R.id.tv_suggest_dynamics_buttom).setOnClickListener(this);
    }

    private void initWidget() {
        mRecyclerView = $(R.id.rcv_suggest_dynamics);

        LinearLayoutManager labelsLayoutManager = new LinearLayoutManager(mContext);
        labelsLayoutManager.setOrientation(LinearLayout.VERTICAL);
        mRecyclerView.setLayoutManager(labelsLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .colorResId(R.color.bg_f5)
                .sizeResId(R.dimen.divider_size_1x)
                .margin((int) getResources().getDimension(R.dimen.size_60dp), 0)
                .build());

        mRecyclerView.setAdapter(mAdapter);
    }

    private void initDatas() {
        loadRecommendUserCommand = ProtoRequestHelper.task(ProtoHttpFactory.getSuggest(20)).setTag(this.hashCode()).request(this);

        mAdapter = new SuggestAdapter();
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        if (command.equals(loadRecommendUserCommand)) {
            Result<List<UserModel>> result = JSON.parseObject(data, new TypeReference<Result<List<UserModel>>>() {
            }.getType());
            if (result.result == ResultCode.CodeSuccess) {
                mAdapter.setNewData(result.data);
                mAdapter.notifyDataSetChanged();
            } else {
                ErrorResult.toastError(this, result.result);
            }
        }
    }

    @Override
    public void onBackPressed() {
        MainActivity2.startActivity(SuggestActivity.this, getIntent().getStringExtra(H5Helper.H5REDIRECTION_KEY));
        finish();
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        ErrorResult.toastError(this, code);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_suggest_dynamics_buttom:
                MainActivity2.startActivity(SuggestActivity.this, getIntent().getStringExtra(H5Helper.H5REDIRECTION_KEY));
                finish();
                break;
        }
    }
}
